<?php
namespace app\common\model;
use think\Cache;

/**
 * 系统设置模型
 */
class WebSet extends BaseModel
{
    protected $name = 'web_set';
    protected $createTime = false;

    /**
     * 获取器: 转义数组格式
     */
    public function getValuesAttr($value)
    {
        return json_decode($value, true);
    }

    /**
     * 修改器: 转义成json格式
     */
    public function setValuesAttr($value)
    {
        return json_encode($value);
    }

    /**
     * 获取指定项设置
     */
    public static function getItem($key)
    {
        $data = self::getAll();
        return isset($data[$key]) ? $data[$key]['values'] : [];
    }

    /**
     * 获取设置项信息
     */
    public static function detail($key)
    {
        return self::useGlobalScope(false)->find(compact('key'));
    }

    /**
     * 全局缓存: 系统设置
     */
    public static function getAll()
    {
        $self = new static;
        if (!$data = Cache::get('web_set_hema')) {
            $data = array_column(collection($self::useGlobalScope(false)->select())->toArray(), null, 'key');
            Cache::set('web_set_hema', $data);
        }
        return array_merge_multiple($self->defaultData(), $data);
    }

    /**
     * 默认配置
     */
    public function defaultData()
    {
        return [
			'web' => [
                'key' => 'web',
                'describe' => '站点设置',
                'values' => [
					'name' => '',//网站名称
					'domain' => '',	//网站域名
					'icp' => '',	//备案号
					'address' => '',//地址
					'keywords' => '',	//关键字
					'description' => '',	//描述
					'phone' => '',//联系电话
					'qrcode' => web_url().'assets/index/no_pic.jpg',//公众号二维码
					'wxmap' => '',	//微信地图KEY
					'copyright' => '©河马云店'	//小程序显示版权
				],
            ],
			'register' => [
                'key' => 'register',
                'describe' => '注册设置',
                'values' => [
					'prefix' => 'hema',//会员账号前缀
					'register' => '注册',	//注册会员回复关键字
					'username' => '找回账号',	//找回账号回复关键字
					'password' => '找回密码',//找回密码回复关键字
				],
            ],
			'pay' => [
                'key' => 'pay',
                'describe' => '收款设置',
                'values' => [
					'app_id' => '',	//公众号APPID
					'mchid' => '',	//商户号
					'apikey' => '',	//密钥
					'examine' => 300	//快速注册小程序审核费用，0为不收取
				],
            ],
			'wxapp' => [
                'key' => 'wxapp',
                'describe' => '商家助手',
                'values' => [
					'app_id' => '',	//公众号APPID
					'app_secret' => ''	//密钥
				],
            ],
			'delivery' => [
                'key' => 'delivery',
                'describe' => '配送接口',
                'values' => [
					'sf' => [
						'app_key' => '',//开发者ID
						'app_secret' => ''//开发者密钥
					],
					'dada' => [
						'app_key' => '',//开发者ID
						'app_secret' => '',//开发者密钥
						'source_id' => '' //商户号
					]
				],
            ],
			'printer' => [
                'key' => 'printer',
                'describe' => '打印设备',
                'values' => [
					//对对机
					'ddj' => [
						'app_key' => '',//开发者ID
						'app_secret' => ''//开发者密钥
					],
					//飞鹅
					'feie' => [
						'app_key' => '',//开发者ID
						'app_secret' => ''//开发者密钥
					],
					//易联云
					'yilian' => [
						'app_key' => '',//开发者ID
						'app_secret' => '',//开发者密钥
						'access_token' => ''//access_token
					],
				],
            ],
			'menus' => [
                'key' => 'menus',
                'describe' => '公众号菜单',
                'values' => [
					0 => [
						"type" => "view",
						"name" => "一级菜单",
						"sub_button" => [
							0 => [
								"type" => "click",
								"name" => "二级菜单",
								"key" => "关键字"
							],
						],
						"url" => web_url()
					]
				],
            ],
			'tplmsg' => [
                'key' => 'tplmsg',
                'describe' => '模板消息',
                'values' => [
					'new_order' => [	//新订单通知
						'template_id' => ''
					],
					'examine' => [	//审核状态通知
						'template_id' => ''
					],
				],
            ],
			'subscribe' => [
                'key' => 'subscribe',
                'describe' => '关注回复',
                'values' => [
                    'is_open' => 0,		//是否开启 0=关闭，1=开启
					'type' => 'text',	//消息类型 text=文字消息,image=图片消息,news=图文消息,voice=声音消息
					'dataGroup' => [
						'text' => [
							'content' => ''
						],
						'image' => [
							'url' => web_url().'/assets/store/img/diy/banner_01.jpg',
							'file_name' => '',
							'media_id' => ''
						],
						'voice' => [
							'media_id' => ''
						],
						'video' => [
							'title' => '',
							'description' => '',
							'media_id' => ''
						],
						'music' => [
							'title' => '',
							'description' => '',
							'url' => '',	//音乐路径
							'hurl' => '',	//高质量音乐路径
							'media_id' => ''	//必须是缩略图的media_id
						],
						'news' => [
							'media_id' => '',
							'item' => [
								/*0 => [
									'title' => '',//标题
									'description' => '',//描述
									'picurl' => '',//图片链接，支持JPG、PNG格式，较好的效果为大图360*200，小图200*200
									'url' => ''//点击图文消息跳转链接
								]*/
							]
							//最多8条
						]
					]
                ]
			],
			'invalid' => [
                'key' => 'invalid',
                'describe' => '无效回复',
                'values' => [
                    'is_open' => 0,		//是否开启 0=关闭，1=开启
					'type' => 'text',	//消息类型 text=文字消息,image=图片消息,news=图文消息,voice=声音消息
					'dataGroup' => [
						'text' => [
							'content' => ''
						],
						'image' => [
							'url' => web_url().'/assets/store/img/diy/banner_01.jpg',
							'file_name' => '',
							'media_id' => ''
						],
						'voice' => [
							'media_id' => ''
						],
						'video' => [
							'title' => '',
							'description' => '',
							'media_id' => ''
						],
						'music' => [
							'title' => '',
							'description' => '',
							'url' => '',	//音乐路径
							'hurl' => '',	//高质量音乐路径
							'media_id' => ''	//必须是缩略图的media_id
						],
						'news' => [
							'media_id' => '',
							'item' => [
								/*0 => [
									'title' => '',//标题
									'description' => '',//描述
									'picurl' => '',//图片链接，支持JPG、PNG格式，较好的效果为大图360*200，小图200*200
									'url' => ''//点击图文消息跳转链接
								]*/
							]
							//最多8条
						]
					]
                ]
			],
        ];
    }

}
